package com.iaiai.browser.activity.widget;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PictureDrawable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.iaiai.browser.activity.R;
import com.iaiai.browser.core.util.Utils;

/**
 * 
 * <p>
 * Title: IndexController.java
 * </p>
 * <p>
 * E-Mail: 176291935@qq.com
 * </p>
 * <p>
 * QQ: 176291935
 * </p>
 * <p>
 * Http: iaiai.iteye.com
 * </p>
 * <p>
 * Create time: 2011-11-2
 * </p>
 * 
 * @author 丸子
 * @version 0.0.1
 */
public class FavoritePanel {

	private Context context;

	private int BACKGROUND = R.drawable.w_bg_with_shadow; // 背景
	private int VERTICAL = R.drawable.column_cover_arrow; // 竖线背景
	private int NONE = R.drawable.none; // 无背景图片

	private LinearLayout textLayout; // 文字布局

	private LinearLayout imgLayout; // 图片布局

	private RelativeLayout imageLayout; // 图片布局
	
	private int height = 100; // 高度

	private TextView textView; // 文字

	private TextView dateView; // 添加日期

	private OnClickListener onclick; // 添加单击事件

	/**
	 * 初始化
	 */
	private void init() {
		textLayout = new LinearLayout(context); // 文字布局
		imgLayout = new LinearLayout(context); // 图片布局

		// 设置文本域
		textLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.FILL_PARENT, 1));
		textLayout.setOrientation(LinearLayout.HORIZONTAL);

		// 设置文本域中的颜色
		LinearLayout colorLayout = new LinearLayout(context);
		colorLayout.setLayoutParams(new LinearLayout.LayoutParams(2, LinearLayout.LayoutParams.FILL_PARENT));
		colorLayout.setBackgroundColor(Color.RED);
		textLayout.addView(colorLayout);

		// 设置文字域中的文字布局
		LinearLayout contentLayout = new LinearLayout(context);
		contentLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.FILL_PARENT));
		contentLayout.setOrientation(LinearLayout.VERTICAL);
		contentLayout.setPadding(Utils.dipTopx(context, 15), Utils.dipTopx(context, 15), Utils.dipTopx(context, 15), Utils.dipTopx(context, 15));
		textLayout.addView(contentLayout);

		// 文字
		textView = new TextView(context);
		textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));
		textView.setTextColor(Color.BLACK);
		textView.setTextSize(Utils.dipTopx(context, 30));
		textView.setSingleLine(true);
		textView.setEllipsize(TextUtils.TruncateAt.END);
		contentLayout.addView(textView);

		dateView = new TextView(context);
		dateView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));
		dateView.setTextColor(Color.GRAY);
		contentLayout.addView(dateView);

		imgLayout.setLayoutParams(new LinearLayout.LayoutParams(Utils.dipTopx(context, 100), LinearLayout.LayoutParams.FILL_PARENT));
		imgLayout.setOrientation(LinearLayout.VERTICAL);

		imageLayout = new RelativeLayout(context);
		imageLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.FILL_PARENT));
		imageLayout.setBackgroundDrawable(context.getResources().getDrawable(NONE));
		imgLayout.addView(imageLayout);
		
		ImageView verticalView = new ImageView(context);
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.FILL_PARENT);
		params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
		verticalView.setLayoutParams(params);
		verticalView.setBackgroundDrawable(context.getResources().getDrawable(VERTICAL));
		imageLayout.addView(verticalView);
	}

	public FavoritePanel(Context context) {
		this.context = context;

		init(); // 初始化
	}

	/**
	 * 设置文字
	 * 
	 * @param content
	 */
	private void setText(String content) {
		textView.setText(content);
	}

	/**
	 * 设置日期
	 */
	private void setDate(String date) {
		dateView.setText(date);
	}

	/**
	 * 获得布局
	 * 
	 * @param content
	 * @return
	 */
	public LinearLayout getWidget(String content,String date) {
		return getWidget(content,date,null);
	}
	
	/**
	 * 获得布局
	 * 
	 * @param content
	 * @return
	 */
	public LinearLayout getWidget(String content,String date,String filename) {
		setText(content);
		if(date!=null)
			setDate(date);
		
		if(filename!=null){
			try {
				FileInputStream inputStream = new FileInputStream(filename);
				imageLayout.setBackgroundDrawable(Drawable.createFromStream(inputStream, "src"));
				inputStream.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return getWidget();
	}

	/**
	 * 获得布局
	 * 
	 * @return
	 */
	public LinearLayout getWidget() {
		LinearLayout layout = new LinearLayout(context);

		layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, Utils.dipTopx(context, height)));
		layout.setOrientation(LinearLayout.HORIZONTAL);
		layout.setBackgroundDrawable(context.getResources().getDrawable(BACKGROUND));

		layout.addView(textLayout);
		layout.addView(imgLayout);

		if(onclick!=null)
			layout.setOnClickListener(onclick);

		return layout;
	}

	/**
	 * 设置单击事件
	 * @param onclick
	 */
	public void setOnClickListener(OnClickListener onclick) {
		this.onclick = onclick;
	}

}
